import router from './router'
import store from './store'
import nProgress from 'nprogress'
import 'nprogress/nprogress.css'

const whiteList = ['/login', '/404']
router.beforeEach(async(to, from, next) => {
  nProgress.start()
  if (store.getters.token) {
    // 二级判断
    if (to.path === '/login') {
      next('/')
    } else {
      // 三级判断
      if (!store.getters.id) {
        const { roles: { menus }} = await store.dispatch('user/getUserInfo')
        router.addRoutes(await store.dispatch('permission/filterRoutes', menus))
        next(to.path)
      } else {
        next()
      }
    }
  } else {
    whiteList.includes(to.path) ? next() : next('/login')
    console.log(1)
  }
  nProgress.done()
})

router.afterEach(() => {
  nProgress.done() // 关闭进度条
})
